package com.lime49.lockcrypt;
import com.lime49.lockcrypt.ui.AbstractDialog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.EtchedBorder;

/**
 *
 */
public class FrmAddType extends AbstractDialog {
    private ArrayList<AccType> types;
    private ResourceBundle lang;
    private boolean editing = false;
    
    private JTextField txtTypeName;
    private JTextArea txtTypeFields;
    private JCheckBox chkNotes;
    
    public FrmAddType(JDialog owner, ResourceBundle lang, boolean editing, AccType type, ArrayList<AccType> types) {
        super(owner, true);
        this.lang = lang;
        this.types = types;
        String frameTitle;
        if(editing) {
            frameTitle = lang.getString("EDITTYPE");
            editing = true;
        } else {
            frameTitle = lang.getString("NEWTYPE");
        }
        this.setTitle(frameTitle);
            //addTypeDialog.setIconImage(this.getIconImage());
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            // close the frame when the user presses escape
            public void actionPerformed(ActionEvent e) {
                thisDialog.dispose();
            }
        }; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
        JPanel addTypeContainer = new JPanel(new GridBagLayout());
        addTypeContainer.setBorder(Constants.EMPTYBORDER);
        this.setContentPane(addTypeContainer);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = Constants.GLOBALINSETS;
        int curY = 0;
        
        Utils.setgrid(c,curY,0,1,1,GridBagConstraints.NONE,GridBagConstraints.FIRST_LINE_START,0.5);
        JLabel lblTypeName = new JLabel(lang.getString("NAME"));
            addTypeContainer.add(lblTypeName,c);
        Utils.setgrid(c,curY,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.FIRST_LINE_END,1);
        txtTypeName = new JTextField();
            lblTypeName.setLabelFor(txtTypeName);
            addTypeContainer.add(txtTypeName,c);
            curY++;
        
        JLabel lblFields = new JLabel(lang.getString("FIELDS"));
        Utils.setgrid(c,curY,0,1,1,GridBagConstraints.NONE,GridBagConstraints.FIRST_LINE_START,0.5);
            addTypeContainer.add(lblFields,c);
        Utils.setgrid(c,curY,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.BOTH,GridBagConstraints.FIRST_LINE_END,1);
        txtTypeFields = new JTextArea(10,25);
            lblFields.setLabelFor(txtTypeFields);
            txtTypeFields.setLineWrap(true);
            txtTypeFields.setWrapStyleWord(true); //wrap at words, not chars
            //txtTypeFields.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            JScrollPane scrollFields = new JScrollPane(txtTypeFields);
            scrollFields.setViewportBorder(null);//BorderFactory.createEmptyBorder());
            scrollFields.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
            addTypeContainer.add(scrollFields,c);
            curY++;
        if(!type.getName().isEmpty()) {
            txtTypeName.setText(type.getName());
            txtTypeFields.setText(Utils.implode(type.getFields(),"\n"));
        }
            
        JLabel lblNotes = new JLabel(lang.getString("NOTES"));
        Utils.setgrid(c,curY,0,1,1,GridBagConstraints.NONE,GridBagConstraints.FIRST_LINE_START,0.5);
            addTypeContainer.add(lblNotes,c);
        Utils.setgrid(c,curY,1,GridBagConstraints.REMAINDER,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.FIRST_LINE_END,1);
        chkNotes = new JCheckBox(lang.getString("SHOWNOTES"));
            chkNotes.setSelected(type.getShowNotes());
            addTypeContainer.add(chkNotes,c);
            curY++;
        
        Utils.setgrid(c,curY,0,2,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.LAST_LINE_END,1.5);
            JPanel buttonbox = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            JButton btnok = new JButton(lang.getString("OK"));
                btnok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) { 
                       if(validateType()) {
                           result = JOptionPane.OK_OPTION;
                           thisDialog.dispose();
                       }
                   } 
                });
            JButton btncancel = new JButton(lang.getString("CANCEL"));
                btncancel.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) { 
                       thisDialog.dispose(); 
                   }
                });
                btnok.setMinimumSize(Constants.MINSIZE);
                btnok.setPreferredSize(Constants.PREFSIZE);
                btnok.setMaximumSize(Constants.MAXSIZE);
                btncancel.setMinimumSize(Constants.MINSIZE);
                btncancel.setPreferredSize(Constants.PREFSIZE);
                btncancel.setMaximumSize(Constants.MAXSIZE);
            buttonbox.add(btnok);
            buttonbox.add(btncancel);
            c.insets = new Insets(0,0,0,0);
            addTypeContainer.add(buttonbox,c);
            this.getRootPane().setDefaultButton(btnok);
        txtTypeFields.setMaximumSize(new Dimension(300,180));
        c.weighty = 1;
        Utils.setgrid(c,curY,0,1,1,GridBagConstraints.BOTH,GridBagConstraints.NORTHWEST,1);
            addTypeContainer.add(Box.createVerticalGlue(),c);
        this.setMinimumSize(new Dimension(200,200));
        this.setMaximumSize(new Dimension(300,200));
        this.pack();
        this.setLocationRelativeTo(this);
        this.setVisible(true);
    }
    
    /**
     * Adds a new type or edits an existing one, depending on the ActionCommand specified in <i>e</i>.
     * Allows for duplicate types and types which have been deleted from the list but still 
     * remain in the database.
     * @param e The ActionEvent created by the Ok button on the showAddTypeDialog form.
     */
    private boolean validateType() {
        String typeName = txtTypeName.getText().trim();
        String typeFieldsInp = txtTypeFields.getText().trim();
        if(typeName.length() == 0) {
            JOptionPane.showMessageDialog(this,lang.getString("ERR_TYPEBLANK"),lang.getString("ERR_TYPENAMEINVALID"), JOptionPane.ERROR_MESSAGE);
            return false;
        } else {
            if(Utils.getTypeIndex(typeName,types,"types") >=0 && (editing)) { // actionCommand is "add", and type exists
                JOptionPane.showMessageDialog(this,lang.getString("ERR_TYPEEXISTS"),lang.getString("ERR_TYPENAMEINVALID"), JOptionPane.ERROR_MESSAGE);
                return false;
            } else if(typeFieldsInp.trim().length() == 0) { // there are no fields
                JOptionPane.showMessageDialog(this,lang.getString("ERR_FIELDSINVALID_DESC"),lang.getString("ERR_FIELDSINVALID"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        return true;
    }
    
    public String getTypeName() {
        return txtTypeName.getText().trim();
    }
    public String getTypeFieldsInp() {
        return txtTypeFields.getText().trim();
    }
    public boolean getShowNotes() {
        return chkNotes.isSelected();
    }
}
